package ch.cm.adnroid.cleverlearn.learn;

import java.net.URI;
import java.util.ArrayList;

import android.app.Activity;
import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Pair;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import ch.cm.adnroid.cleverlearn.CleverLearnStart;
import ch.cm.adnroid.cleverlearn.R;
import ch.cm.adnroid.cleverlearn.R.id;
import ch.cm.adnroid.cleverlearn.learn.util.QuestionPicker;
import ch.cm.adnroid.cleverlearn.load.CleverLearnLoad;
import ch.cm.adnroid.cleverlearn.model.DatabaseManager;
import ch.cm.adnroid.cleverlearn.model.Question;

public class CleverLearnLearn extends Activity {
    
    private TextView questionText;
    private Button answer1, answer2, answer3;
    private ImageView imageView;
    private QuestionPicker picker;
    private Question question;
    private ArrayList<Pair<String, Boolean>> answers;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_clever_learn_learn);

        
        questionText  = (TextView) findViewById(R.id.tx_learn_question);
        answer1 = (Button) findViewById(R.id.bu_learn_ansver_a);
        answer2 = (Button) findViewById(R.id.bu_learn_ansver_b);
        answer3 = (Button) findViewById(R.id.bu_learn_ansver_c);
       // imageView = (ImageView) findViewById(R.id.pic_learn_questionpic);
    }

    @Override
    protected void onResume() {
        super.onResume();
        try {
            picker = new QuestionPicker(new DatabaseManager(this).getReadableDatabase(),this);
            initialise();
        } catch (Exception e) {
           Toast.makeText(this, "Keine Fragen gefunden", Toast.LENGTH_SHORT).show();
           super.onDestroy();
        }
    }
    
    
    private void initialise() {
        question = picker.pickAQuestion(getIntent().getExtras().getString(CleverLearnStart.TABLE_KEY));
        questionText.setText(question.getQuestion());
        if (question.getPicName()!="") {
            // add code here
            
        }
         answers = question.getAnswerMap();
         answer1.setText(answers.get(0).first);
         answer2.setText(answers.get(1).first);
         answer3.setText(answers.get(2).first);
         enableAllButtons();
    }
    
    private void enableAllButtons() {
        answer1.setEnabled(true);
        answer2.setEnabled(true);
        answer3.setEnabled(true); 
    }

    public void onButtonClick(View view) {
        switch (view.getId()) {
        case id.bu_learn_ansver_a:
            checkAnswer(answer1, 0);
            break;
        case id.bu_learn_ansver_b:
            checkAnswer(answer2, 1);
            break;
        case id.bu_learn_ansver_c:
            checkAnswer(answer3, 2);
            break;
        }
    }

    private void checkAnswer(Button b, int element) {
        if (answers.get(element).second) {
            question.setrACount(question.getrACount()+1);
            updateQuestionDB();
            Toast.makeText(this, "Richtig", Toast.LENGTH_SHORT).show();
            initialise();
        }else {
            question.setrACount(question.getrACount()-1); 
            Toast.makeText(this, "leider Nein", Toast.LENGTH_SHORT).show();
            b.setEnabled(false);
        }
    }
    
    private void updateQuestionDB() {
        SQLiteDatabase database = new DatabaseManager(this).getWritableDatabase();
        
        ContentValues values = new ContentValues();
        values.put(DatabaseManager.QUESTION_TABLE_COLUMN_RIGHTCOUNT, question.getrACount());
        String WHERE = DatabaseManager.QUESTION_TABLE_COLUMN_ID +" = "+question.getId();
        database.update(getIntent().getExtras().getString(CleverLearnStart.TABLE_KEY), values, WHERE, null);
    }
}
